import { hideLoading, showLoading } from './loading';
import { MyRequestOptions, HttpResponse } from './type';
/**
 * 封装uni.request
 * @param {String}  option.method  请求方式
 * @param {String}  option.url     请求url
 * @param {Object}  option.data    请求参数
 * @param {Object}  option.loading loading配置信息
 * @param {Boolean} option.loading.show 显示loading
 * 余下配置参考 RequestOptions 类型定义
 * 如果要扩展 可在 MyRequestOptions 下进行类型扩展
 * 
 * ==== 
 * MyRequestOptions  HttpResponse 放在了./type/index.ts 建议放在 global.d.ts 全局 ts 类型定义中
 * ====
 * @returns
 */
const request = <T = any>(
	option : MyRequestOptions
) : Promise<HttpResponse<T>> => {
	return new Promise((resolve, reject) => {
		// 处理请求loading
		if (option.loading?.show) {
			showLoading(option.loading.option);
		}
		uni.request({
			method: option.method || 'GET',
			url: `${getApp().globalData.baseUrl}${option.url}`, // 完整URL
			data: option.data || option.params || {},
			header: {
				'content-type': option.header ? option.header : 'application/json',
				token: uni.getStorageSync('token')
			},
			success: async (res) => {
				// console.log(`${getApp().globalData.baseUrl}${option.url}`)
				// console.log(option.data || {})
				// console.log(res)
				// 处理响应数据
				if (option.loading?.show) {
					hideLoading();
				}
				// ========= 下面按要求后端的返回自行处理 ========
				const data = res.data as HttpResponse<T>;
				if (data.code === 200) {
					resolve(data);
				} else if (data.code === 500) {
					uni.showToast({
						title: data.msg,
						icon: 'none'
					});
					reject(data);
				} else if (data.code === 401) {
					
					// uni.showToast({
					// 	title:'登录已失效!',
					// 	icon:'error'
					// })
					uni.navigateTo({
						url: '/pages/login/login'
					});
				} else if (data.code === 40200) {
					uni.showToast({
						title:data.msg,
						icon:'none'
					})
					setTimeout(()=>{
						uni.navigateTo({
							url: '/pages/login/login'
						})
					},500)
				}
				
				else {
					uni.showToast({
						title: data.msg,
						icon: 'none'
					});
					reject(data);
				}
				// ========= 上面按要求后端的返回自行处理 ========
			},
			fail: (err) => {
				if (option.loading?.show) {
					hideLoading();
				}
				if (err.errMsg.includes('request:fail abort statusCode:-1')) {
					// 网络异常
					uni.showToast({
						title: '服务器连接异常,请检查网络,或稍后再试!',
						icon: 'none',
						duration: 2000
					});
				} else {
					// 其他错误处理
					uni.showToast({
						title: err.errMsg,
						icon: 'none'
					});
				}

				reject(err);
			}
		});
	});
};

// 这里仅封装了部分常用的请求方法，可以根据需求继续封装其他方法
request.post = <T = any>(option : MyRequestOptions) => {
	return request({
		method: 'POST',
		...option
	}) as unknown as Promise<HttpResponse<T>>;
};

request.get = <T = any>(option : MyRequestOptions) => {
	return request({
		method: 'GET',
		...option
	}) as unknown as Promise<HttpResponse<T>>;
};

request.put = <T = any>(option : MyRequestOptions) => {
	return request({
		method: 'PUT',
		...option
	}) as unknown as Promise<HttpResponse<T>>;
};

request.delete = <T = any>(option : MyRequestOptions) => {
	return request({
		method: 'DELETE',
		...option
	}) as unknown as Promise<HttpResponse<T>>;
};

export default request;